package com.axend.aerosense.dev.viewmodel;

import android.app.Activity;

import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.common.bean.MoveBindRoomBean;
import com.axend.aerosense.common.bean.GetRadarList;
import com.blankj.utilcode.util.ToastUtils;
import com.axend.aerosense.base.bean.BaseDevInfoBean;
import com.axend.aerosense.base.bean.BaseStringBean;
import com.axend.aerosense.base.impl.IBaseView;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.impl.IOperationResult;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.base.viewmodel.CustomBaseViewModel;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.ui.LoadingDialog;
import com.axend.aerosense.common.util.JsonUtil;
import com.axend.aerosense.network.EasyHttp;
import com.axend.aerosense.network.cache.model.CacheMode;
import com.axend.aerosense.network.callback.ProgressDialogCallBack;
import com.axend.aerosense.network.callback.SimpleCallBack;
import com.axend.aerosense.network.exception.ApiException;
import com.axend.aerosense.dev.R;

import io.reactivex.disposables.Disposable;

public class RadarsViewModel extends CustomBaseViewModel<GetRadarList> {
    private Disposable disposable;

    public RadarsViewModel(IBaseView baseViewImpl, ILoadDataResult<GetRadarList> loadDataResultImpl) {
        super(baseViewImpl, loadDataResultImpl);
    }

    @Override
    public void onLoad() {
        String token = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN);
        disposable = EasyHttp.post(UrlApi.URL.GetAllDev.getUrl())
                .headers("token", token)
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(new BaseDevInfoBean()))
                .execute(new SimpleCallBack<GetRadarList>() {
                    @Override
                    public void onTokenExpire() {
                        //登录界面不做处理
                        loadDataFail("");
                        com.blankj.utilcode.util.ToastUtils.showShort(R.string.common_login_first);
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }

                    @Override
                    public void onError(ApiException e) {
                        ToastUtils.showShort(e.getMessage());
                        loadDataFail(e.getMessage());
                    }

                    @Override
                    public void onSuccess(GetRadarList bean) {
                        loadDataSuccess(bean);
                    }
                });
    }

    public void devsBindRoom(Activity activity, MoveBindRoomBean bean, IOperationResult operationResult){
        String token = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN);
        disposable = EasyHttp.post(UrlApi.URL.MoveRadars.getUrl())
                .headers("token", token)
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(bean))
                .execute(new ProgressDialogCallBack<BaseStringBean>(LoadingDialog.getProgressDialog(activity)) {
                    @Override
                    public void onTokenExpire() {
                        dismissProgress();
                        com.blankj.utilcode.util.ToastUtils.showShort(R.string.common_login_first);
                        //登录界面不做处理
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }

                    @Override
                    public void onError(ApiException e) {
                        dismissProgress();
                        e.printStackTrace();
                        ToastUtils.showShort(e.getMessage());
                        if(null != operationResult){
                            operationResult.onFail();
                        }
                    }

                    @Override
                    public void onSuccess(BaseStringBean bean) {
                        dismissProgress();
                        ToastUtils.showShort(R.string.common_success);
                        if(null != operationResult){
                            operationResult.onSuccess();
                        }
                    }
                });
    }

    public void deleteRadars(Activity activity, MoveBindRoomBean bean, IOperationResult operationResult){
        String token = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN);
        disposable = EasyHttp.post(UrlApi.URL.DeleteRadars.getUrl())
                .headers("token", token)
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(bean))
                .execute(new ProgressDialogCallBack<BaseStringBean>(LoadingDialog.getProgressDialog(activity)) {
                    @Override
                    public void onTokenExpire() {
                        dismissProgress();
                        com.blankj.utilcode.util.ToastUtils.showShort(R.string.common_login_first);
                        //登录界面不做处理
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }

                    @Override
                    public void onError(ApiException e) {
                        dismissProgress();
                        e.printStackTrace();
                        ToastUtils.showShort(e.getMessage());
                        if(null != operationResult){
                            operationResult.onFail();
                        }
                    }

                    @Override
                    public void onSuccess(BaseStringBean bean) {
                        dismissProgress();
                        ToastUtils.showShort(R.string.common_success);
                        if(null != operationResult){
                            operationResult.onSuccess();
                        }
                    }
                });
    }

    @Override
    protected void onCleared() {
        if(null != disposable && !disposable.isDisposed()){
            disposable.dispose();
        }
        super.onCleared();
    }
}
